package com.ruoyi.patrol.controller;

import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.patrol.domain.vo.PtFormDetailVo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.patrol.domain.PtForm;
import com.ruoyi.patrol.service.IPtFormService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 单Controller
 * 
 * @author lkm
 * @date 2022-11-05
 */
@RestController
@RequestMapping("/patrol/form")
public class PtFormController extends BaseController
{
    @Autowired
    private IPtFormService ptFormService;

    /**
     * 查询单列表
     */
    @PreAuthorize("@ss.hasPermi('patrol:form:list')")
    @GetMapping("/list")
    public TableDataInfo list(PtForm ptForm)
    {
        startPage();
        List<PtForm> list = ptFormService.selectPtFormList(ptForm);
        return getDataTable(list);
    }

    /**
     * 查询单列表（不分页）
     */
    @PreAuthorize("@ss.hasPermi('patrol:form:list')")
    @GetMapping("/list/nopage")
    public List<PtForm> listNoPage(PtForm ptForm)
    {
        List<PtForm> list = ptFormService.selectPtFormList(ptForm);
        return list.stream().filter(form -> form.getFillingX() > 0).collect(Collectors.toList());
    }

    /**
     * 导出单列表
     */
    @PreAuthorize("@ss.hasPermi('patrol:form:export')")
    @Log(title = "单", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(PtForm ptForm)
    {
        List<PtForm> list = ptFormService.selectPtFormList(ptForm);
        ExcelUtil<PtForm> util = new ExcelUtil<PtForm>(PtForm.class);
        return util.exportExcel(list, "form");
    }

    /**
     * 获取单详细信息
     */
    @PreAuthorize("@ss.hasPermi('patrol:form:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(ptFormService.selectPtFormById(id));
    }

    /**
     * 新增单
     */
    @PreAuthorize("@ss.hasPermi('patrol:form:add')")
    @Log(title = "单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PtFormDetailVo ptFormDetailVo)
    {
        return toAjax(ptFormService.insertPtForm(ptFormDetailVo));
    }

    /**
     * 修改单
     */
    @PreAuthorize("@ss.hasPermi('patrol:form:edit')")
    @Log(title = "单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PtForm ptForm)
    {
        return toAjax(ptFormService.updatePtForm(ptForm));
    }

    /**
     * 删除单
     */
    @PreAuthorize("@ss.hasPermi('patrol:form:remove')")
    @Log(title = "单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(ptFormService.deletePtFormByIds(ids));
    }
}
